// *************************************************************************************************
//
// MK3D Engine
// --------------------------------------
// Copyright (C) 2007-2011 Zeb (mailto: zebbey@gmail.com)
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// *************************************************************************************************

#include "mk_header.h"
#include "mk_mp_alpha_blend.h"
#include "mk_scene.h"
#include "mk_engine.h"
#include "mk_render_device.h"

namespace MK3DE
{
	BOOL CMPAlphaBlend::_s_DefaultEnable = FALSE;
	BLEND_PARAM CMPAlphaBlend::_s_DefaultSrcBlend = BP_SRC_ALPHA;
	BLEND_PARAM CMPAlphaBlend::_s_DefaultDestBlend = BP_INV_SRC_ALPHA;

	CMPAlphaBlend::CMPAlphaBlend()
	{
		_bEnable = _s_DefaultEnable;
		_srcBlend = _s_DefaultSrcBlend;
		_destBlend = _s_DefaultDestBlend;
	}

	void CMPAlphaBlend::SetAlphaBlend(BOOL bEnable, BLEND_PARAM srcBlend, BLEND_PARAM destBlend)
	{
		_bEnable = bEnable;
		_srcBlend = srcBlend;
		_destBlend = destBlend;

		if (bEnable != _s_DefaultEnable)
			_bNeedToSet = TRUE;
		else
			_bNeedToSet = FALSE;
	}

	void CMPAlphaBlend::GetAlphaBlend(BOOL& bEnable, BLEND_PARAM& srcBlend, BLEND_PARAM& destBlend)
	{
		bEnable = _bEnable;
		srcBlend = _srcBlend;
		destBlend = _destBlend;
	}

	void CMPAlphaBlend::OnSetRenderState()
	{
		if (_bNeedToSet)
		{
			CRenderDevice* renderDevice = GetEngine()->GetRenderDevice();
			renderDevice->SetRenderState(RS_ALPHA_BLEND_ENABLE, _bEnable);
			renderDevice->SetRenderState(RS_SRC_BLEND, _srcBlend);
			renderDevice->SetRenderState(RS_DEST_BLEND, _destBlend);
		}
	}

	void CMPAlphaBlend::OnResetRenderState()
	{
		if (_bNeedToSet)
		{
			CRenderDevice* renderDevice = GetEngine()->GetRenderDevice();
			renderDevice->SetRenderState(RS_ALPHA_BLEND_ENABLE, _s_DefaultEnable);
			renderDevice->SetRenderState(RS_SRC_BLEND, _s_DefaultSrcBlend);
			renderDevice->SetRenderState(RS_DEST_BLEND, _s_DefaultDestBlend);
		}
	}
}